/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public class Reasoning implements IElement {

    public static final String NAME = "Reasoning";
    private INode parentNode;

    private LinkedList<ReasoningItem> reasoningItemList = new LinkedList<ReasoningItem>();
    private PerCasesReasoning perCasesReasoning = null;
    private EndPosition endPosition = null;
	
	public Reasoning(Proof proof) {
		this.parentNode = proof;
	}

	public Reasoning(Now now) {
		this.parentNode = now;
	}

	public void pollElement(LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        XMLElement xmlElement = xmlElementList.peek();
        if (xmlElement == null){
            throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
        }
        if (xmlElement.getName().equals("PerCasesReasoning")){
            if (this.perCasesReasoning == null) {
            	this.perCasesReasoning = PerCasesReasoning.newInstance(this, xmlElementList);
            } else { 
                throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <PerCasesReasoning> in element " + NAME, xmlElementList);
            }
        } else if (xmlElement.getName().equals("EndPosition")){
            if (this.endPosition == null) {
            	this.endPosition = EndPosition.newInstance(this, xmlElementList);
            } else { 
                throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <EndPosition> in element " + NAME, xmlElementList);
            }
        } else {
        	ReasoningItem reasoningItem = ReasoningItem.newInstance(this, xmlElementList);
        	this.reasoningItemList.add(reasoningItem);
        } 
	
	}

	@Override
	public INode getParentNode() {
		return this.parentNode;
	}

	@Override
	public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        for (ReasoningItem reasoningItem : this.reasoningItemList){
            xmlElementList.addAll(reasoningItem.getXMLElementList());
        }
        if (this.perCasesReasoning != null){
            xmlElementList.addAll(this.perCasesReasoning.getXMLElementList());
        }
        if (this.endPosition != null){
            xmlElementList.addAll(this.endPosition.getXMLElementList());
        }
		return xmlElementList;
	}

	public LinkedList<ReasoningItem> getReasoningItemList() {
		return reasoningItemList;
	}

	public PerCasesReasoning getPerCasesReasoning() {
		return perCasesReasoning;
	}

	public EndPosition getEndPosition() {
		return endPosition;
	}

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(ReasoningItem reasoningItem: this.reasoningItemList) {
				reasoningItem.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
